/*
 * @Author: lee
 * @Date: 2023-10-25 16:00:27
 * @LastEditors: lee
 * @LastEditTime: 2023-10-25 19:28:11
 * @Description: file content
 */
const {
	articleModel
} = require('../models/article.model')

const {
	userModel
} = require('../models/user.model')

module.exports = class Article {

	//创建文章
	static async addArticle(ctx) {
		const {
			title,
			body,
			description,
			tagId
		} = ctx.request.body;
		if (!(title || body)) {
			return ctx.fail({
				msg: '有未填项',
				code: 401
			})
		}
		let author = ctx.user;

		const newArticle = new articleModel({
			...ctx.request.body, // 任何Schema中未定义的键/值总是被忽略
			author: author._id,
			tagList: [tagId]
		})
		let res = await newArticle.save()
		if (res) {
			return ctx.success({
				msg: '创建成功'
			})
		} else {
			return ctx.success({
				msg: '创建失败',
				code: 201
			})
		}


	}

	//修改文章
	static async updateArticle(ctx) {
		const newArticle = ctx.request.body;

		const article = await articleModel.findById(newArticle.id);

		if (newUser.title != undefined) {
			article.title = newArticle.title;
		}
		if (newArticle.body != undefined) {
			article.body = newArticle.body;
		}
		if (newArticle.description != undefined) {
			article.description = newArticle.description;
		}

		await article.save();
		return ctx.success({
			msg: '修改成功',
			data: article.toJSONFor(ctx.user.id)
		})
	}

	//获取当前文章
	static async currentArticle(ctx) {
		let id = ctx.request.query.id
		let target = await articleModel.findById(ctx.request.query.id)
		//上一篇
		let prev = await articleModel.findOne({
			'_id': {
				'$lt': id
			}
		})
		//下一篇
		let next = await articleModel.findOne({
			'_id': {
				'$gt': id
			}
		})
		if (target) {

			await target.increaseRead();

			let article = {
				title: target.title,
				id: target._id,
				description: target.description,
				body: target.body,
				author: target.author,
				read: target.read,
				authorInfo: await target.getAuthorInfo(target.author)
			}

			if (ctx.user) {
				article.favorited = target.assertFavorite(ctx.user.id) //判断登录用户是否收藏该文章
			}
			return ctx.success({
				data: {
					article,
					next,
					prev
				}
			})
		} else {
			ctx.fail({
				msg: '文章不存在',
				code: 201
			})
		}

	}

	//全部文章
	static async allArticle(ctx) {
		let sort = {createdAt:-1}

		let page = 1;
		let size = 10

		let query = {}

		if (ctx.request.query.size != undefined) {
			size = ctx.request.query.size;
		}
		if (ctx.request.query.page != undefined) {
			page = ctx.request.query.page;
		}

		if (ctx.request.query.tagId != undefined) {
			let tagId = ctx.request.query.tagId
			query.tagList = {
				$in: [tagId]
			};
		}

		//按照排序规则
		if (ctx.request.query.sort != undefined) {
			let _sort = ctx.request.query.sort;
			switch (_sort) {
				case 'created':
					sort.createdAt = -1
					break;
				case 'star':
					sort.favoritedList = -1
					break;
				case 'read':
					sort.read = -1
					break;
				default:
					break;
			}
		}
		//分页
		let articles = await articleModel.find(query)
			.skip(Number(page - 1) * Number(size))
			.limit(Number(size))
			.sort(sort)
		let total = await articleModel.count()
		return ctx.success({
			msg: '获取成功',
			data: {
				articles: articles,
				total
			}
		})
	}

	//获取用户发布的文章
	static async userAticle(ctx) {
		let page = 1;
		let size = 10
		let sort = {createdAt:-1}
		let {
			id
		} = ctx.request.query
		
		if(!id){
			return ctx.fail({msg:'用户id不能为空',code:401})
		}
		//按照排序规则
		if (ctx.request.query.sort != undefined) {
			let _sort = ctx.request.query.sort;
			switch (_sort) {
				case 'created':
					sort.createdAt = -1
					break;
				case 'star':
					sort.favoritedList = -1
					break;
				case 'read':
					sort.read = -1
					break;
				default:
					break;
			}
		}
		
		if (ctx.request.query.size != undefined) {
			size = ctx.request.query.size;
		}

		if (ctx.request.query.page != undefined) {
			page = ctx.request.query.page;
		}
		//分页
		let articles = await articleModel.find({
				author: id
			})
			.skip(Number(page - 1) * Number(size))
			.limit(Number(size))
			.sort(sort)
			
			
		console.log('articles--',articles)
		//如果登录
		if (ctx.user) {
			const user = await userModel.findById(ctx.user.id);
			articles = articles.map(article => article.toJSONFor(user))
		}
		return ctx.success({
			msg: '获取成功',
			data: {
				articles,
				total: articles.length
			}
		})
	}

	//收藏文章
	static async addFavorite(ctx) {
		let {
			id
		} = ctx.request.body
		const article = await articleModel.findById(id);
		await article.addFavorite(ctx.user.id)
		return ctx.success({
			msg: '收藏成功'
		})
	}

	//删除文章
	static async deleteArticle(ctx) {
		let {
			id
		} = ctx.request.body
		const res = await articleModel.remove({
			_id: id
		});
		if (res.ok == 1) {
			return ctx.success({
				msg: '删除成功'
			})
		}
	}

	//获取用户收藏的文章
	static async favoreteArticle(ctx) {
		let query = {}
		const {
			id
		} = ctx.request.query
		let page = 1;
		let size = 10


		if (ctx.request.query.size !== 'undefined') {
			size = ctx.request.query.size;
		}

		if (ctx.request.query.page !== 'undefined') {
			page = ctx.request.query.page;
		}

		let articles = await articleModel.find({
				"favoritedList": {
					"$in": [id]
				}
			})
			.skip(Number(page - 1) * Number(size))
			.limit(Number(size))
			.sort({
				creatAt: -1
			})

		if (ctx.user) {
			const user = await userModel.findById(ctx.user.id);
			articles = articles.map(article => article.toJSONFor(user))
		}
		return ctx.success({
			msg: '获取成功',
			data: {
				articles,
				total: articles.length
			}
		})
	}

}
